//
//  XYXEmoticonTipView.swift
//  SinaWebo
//
//  Created by 亚杏 on 2017/6/15.
//  Copyright © 2017年 oms. All rights reserved.
//

import UIKit
import pop

//表情选择提示视图
class XYXEmoticonTipView: UIImageView {

    //之前选择的表情
    private var preEmoticon: XYXEmoticonModel?

    //提示视图的表情模型
    var emoticon: XYXEmoticonModel? {
        didSet {
            //判断表情是否变化 
            if emoticon == preEmoticon {
                return
            }

            preEmoticon = emoticon

            //设置表情数据
            tipButton.setTitle(emoticon?.emoji , for: .normal)
            tipButton.setImage(emoticon?.image, for: .normal)

            //设置表情动画 - 弹力动画结束时间根据速度自动计算的，不需要指定duration
            let anim = POPSpringAnimation(propertyNamed: kPOPLayerPositionY)
            anim?.fromValue = 30
            anim?.toValue = 8
            anim?.springBounciness = 20
            anim?.springSpeed = 20

            tipButton.layer.pop_add(anim, forKey: nil)


        }
    }


    private lazy var tipButton = UIButton()

    init() {

        let bundle = XYXEmoticonManager.shared.bundle
        let image = UIImage(named: "emoticon_keyboard_magnifier", in: bundle, compatibleWith: nil)
        
        super.init(image: image)

        //设置锚点
        layer.anchorPoint = CGPoint(x: 0.5, y: 1.2)

        //添加按钮
        tipButton.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        tipButton.frame = CGRect(x: 0, y: 8, width: 36, height: 36)
        tipButton.center.x = bounds.width * 0.5
        tipButton.setTitle("ha", for: .normal)
        tipButton.titleLabel?.font = UIFont.systemFont(ofSize: 20)
        addSubview(tipButton)
    }


    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }



}
